package com.zmn.mcc.admin.controller.staff.staffleaveoffice;


import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.request.RequestUtil;
import com.zmn.mcc.common.dto.exception.MccException;
import com.zmn.mcc.model.entity.staff.staffleaveoffice.StaffLeaveOffice;
import com.zmn.mcc.services.interfaces.staff.StaffService;
import com.zmn.mcc.services.interfaces.staff.staffleaveoffice.StaffLeaveOfficeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Optional;


/**
 * 离职信息表基础修改Controller
 *
 * @author tanghongjun
 * @since 2020-05-14 11:15:18
 */
@RestController
@RequestMapping("/staff/staffleaveoffice")
public class StaffLeaveOfficeModifyController extends StaffLeaveOfficeBaseController {
    private Logger logger = LoggerFactory.getLogger(StaffLeaveOfficeListController.class);

    @Resource
    private StaffLeaveOfficeService staffLeaveOfficeService;

    @Resource
    private StaffService staffService;

    /**
     * 保存
     */
    @RequestMapping("/addEdit")
//    @RequiresPermissions("staff/staffleaveoffice/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody StaffLeaveOffice staffLeaveOffice) {

        Date now = DateUtil.getNow();

        int leaveId = Optional.ofNullable(staffLeaveOffice.getLeaveId()).orElse(0);
        String ip = RequestUtil.getRequestIp(request);
        try {
            if (leaveId == 0) {
                staffLeaveOffice.setCreater(getStaffName());
                staffLeaveOffice.setCreateTime(now);
                staffLeaveOffice.setUpdater(getStaffName());
                staffLeaveOffice.setUpdateTime(now);
                staffLeaveOfficeService.insert(staffLeaveOffice);

                staffService.leaveStaff(staffLeaveOffice.getStaffId(), getLoginStaff(), ip);
            } else {
                staffLeaveOffice.setUpdater(getStaffName());
                staffLeaveOffice.setUpdateTime(now);
                staffLeaveOfficeService.updateStaffLeaveOfficeByKey(staffLeaveOffice);
            }

            return ResponseDTO.success(true, "操作成功！");
        } catch (MccException ex) {
            return ResponseDTO.fail(ex.getMessage());
        } catch (Exception e) {
            logger.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail(StatusConsts.STATUS_ERROR, "操作失败！");
        }
    }

    /**
     * 删除
     *
     * @param leaveId
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
//    @RequiresPermissions("staff/staffleaveoffice/delete")
    public ResponseDTO delete(@RequestParam(value = "leaveId", defaultValue = "0") Integer leaveId) {
        if (NumberUtil.isNullOrZero(leaveId)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }
        staffLeaveOfficeService.deleteByKey(leaveId);
        return ResponseDTO.success("删除成功！");
    }

}
